import datetime
from os import abort

from flask import current_app, render_template, request, g, jsonify
from flask import redirect
from flask import session
from flask import url_for

from info import constants
from info.models import House, User
from info.modules import main
from info.modules.main import main_blu
from info.utils.common import user_login
from info.utils.response_code import RET


@main_blu.route('/search')
def search():
    """
    主页面搜索
    """
    area_id = request.args.get('aid')
    start_data = request.args.get('sd')
    end_data = request.args.get('ed')
    if not all([area_id, start_data, end_data]):
        return render_template('index.html')
    try:
        area_id = int(area_id)
    except Exception as e:
        abort(404)
    accommodation = datetime.datetime.strptime(end_data, "%Y-%m-%d") - datetime.datetime.strptime(start_data,
                                                                                                  "%Y-%m-%d")
    time = getattr(accommodation, 'days')
    house = House.query.filter(House.area_id == area_id, House.min_days <= time, House.max_days >= time).order_by(
        House.create_time.desc()).paginate(1, constants.HOUSE_DETAIL_COMMENT_DISPLAY_COUNTS)

    houses = [h.to_basic_dict() for h in house.items]
    # print(houses)
    return render_template('house/search.html', houses=houses)


@main_blu.route('/')
def change():
    """
    渲染主页
    """
    return render_template('index.html')


@main_blu.route('/index.html')
def index():
    return render_template('index.html')


@main_blu.route('/my.html')
def my():
    """
    渲染 我的爱家
    :return:
    """
    return current_app.send_static_file('html/my.html')


@main_blu.route('/profile.html')
def profile():
    """
    渲染 修改个人信息
    :return:
    """
    return current_app.send_static_file('html/profile.html')


@main_blu.route('/orders.html')
def order():
    """
    我的订单
    :return:
    """
    return current_app.send_static_file("html/orders.html")


@main_blu.route('/booking.html')
def booking():
    """
    预定页面渲染
    :return:
    """
    return current_app.send_static_file('html/booking.html')


@main_blu.route("/myhouse.html")
@user_login
def houses():
    user = g.user
    if not user:
        return redirect("/login")
    return current_app.send_static_file("html/myhouse.html")


@main_blu.route('/auth.html')
@user_login
def auth():
    user = g.user
    # session.pop("user_id")
    if not user:
        return redirect("/login")
    return current_app.send_static_file("html/auth.html")


@main_blu.route('/detail.html')
@user_login
def house_detail():
    """
    房屋详情渲染
    :return:
    """
    return current_app.send_static_file('html/detail.html')


@main_blu.route('/register.html')
def register():
    return current_app.send_static_file('html/register.html')


@main_blu.route('/login.html')
def login():
    return current_app.send_static_file('html/login.html')


@main_blu.route('/lorders.html')
@user_login
def lorders():
    """
    客户订单
    :return:
    """
    user = g.user
    if not user:
        return redirect('/login.html')
    return current_app.send_static_file('html/lorders.html')


@main_blu.route('/newhouse.html')
@user_login
def newhouse():
    user = g.user
    if not user:
        return redirect('/login.html')
    return current_app.send_static_file('html/newhouse.html')


@main_blu.route('/favicon.ico')
def favicon():
    return current_app.send_static_file('favicon.ico')
